package gob.inei.enaho.model.service;

import org.xmlpull.v1.XmlPullParser;

import util.file.xml.RegistroXml;
import util.file.xml.TablaXml;
import util.www.sql.SqlUtil;

public abstract class AbstractDataXmlService 
extends AbstractService
{

	
	public void parsear(XmlPullParser parser, TablaXml<RegistroXml> t, String tabla) throws Exception {
		
        RegistroXml rxml = null;
        String campo = null;
        boolean finTabla = false;
        
        int eventType = parser.getEventType();
        
		while (eventType != XmlPullParser.END_DOCUMENT && !finTabla) {
        	
            String tagname = parser.getName();
            switch (eventType) {
            case XmlPullParser.START_TAG:
            	//System.out.println("tagname="+tagname);                	
            	if(tagname.equalsIgnoreCase(tabla)) {
            		//t = new TablaXml<RegistroXml>();   
            		t.setNombre(tagname);
            	}
            	else if(tagname.equalsIgnoreCase("Registro")) {
            		rxml = new RegistroXml();   
            		t.addRegistro(rxml);
            	}
            	else if(rxml!=null) {
            		campo = SqlUtil.codificarString(tagname);
            	}
                break; 
            case XmlPullParser.TEXT:
            	String text = parser.getText();
            	//System.out.println("text="+text);
            	rxml.addValor(campo, text);
                break; 
            case XmlPullParser.END_TAG:
            	//System.out.println("tagname="+tagname);
            	if(tagname.equalsIgnoreCase(tabla)) {
            		finTabla = true;                		
            	}
                break; 
            default:
                break;
            }
            
            eventType = parser.next();
            
        }
	}

	
}
